/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "pdf_stream.h"
#include <string.h>
#include <stdio.h>


PdfMemoryStream::PdfMemoryStream( uint_t bufferSize )
    : m_sp( 0 ),
    m_buffer( NULL )
{
    m_buffer = new byte_t[ bufferSize ];
    memset( m_buffer, 0, bufferSize );
}

PdfMemoryStream::~PdfMemoryStream()
{
    //delete m_buffer;
}

void PdfMemoryStream::write( byte_t* data, uint_t size )
{
    ::memcpy( m_buffer + m_sp, data, size );
    m_sp += size;
}

void PdfMemoryStream::writeByte( byte_t data )
{
    ::memcpy( m_buffer + m_sp, &data, 1 );
    m_sp++;
}

void PdfMemoryStream::saveToFile( char* filename )
{
    FILE *pfile = NULL;
    errno_t result = ::fopen_s( &pfile, filename, "wb");

    ::fwrite( m_buffer, sizeof( byte_t ), m_sp, pfile );

    ::fclose( pfile );
}

unsigned int PdfMemoryStream::getData( void** data )
{
    *data = m_buffer;
    return m_sp;
}


PdfFileStream::PdfFileStream( wchar_t* filename )
    : m_sp( 0 )
{
    errno_t result = ::_wfopen_s( &m_handle, filename, L"wb");
    if ( result )
    {
    }
}

PdfFileStream::~PdfFileStream()
{
    //delete m_buffer;
}

void PdfFileStream::write( byte_t* data, uint_t size )
{
    ::fwrite( data, 1, size, m_handle );
    m_sp += size;
}

void PdfFileStream::writeByte( byte_t data )
{
    this->write( &data, 1 );
}
